// Created by liusc on 2020/8/5-9:24.
import { getDict } from "~/utils";
import {getItem2, getObj, putObj2} from "@/views/wy-budget/api/budgetapply.js";
import { getFiles, checkFiles } from "@/views/wy-budget/api/fileoperation.js";
import uploadMedia from "@/components/upload-media";
import BaseLoad from "@/assets/vue-mixin/BaseLoad";
import fileUpload from '@/views/wy-budget/components/fileUpload'

export default {
    name: "apply_add",
    mixins: [BaseLoad],
    components: { uploadMedia,fileUpload },

    data () {
        var doubleValidate = (rule, value, callback) => {
            let reg = /^[0-9]+(.[0-9]{1,2})?$/;
            let lable = rule.message;
            if (!reg.test(value)) {
                return callback(new Error("数值格式错误"));
            } else {
                callback();
            }
        };

        return {
            fileId:'',
            fileId2:'',
            isShow: false,
            fileList: [],
            modelType: "budgetApply",
            busId: "",

            form: {
                applyData: {
                    applyId: "",
                    budgetId: "",
                    budgetCode: "",
                    applyName: "",
                    applyDept: "",
                    applyType: "",
                    budgetSum: "",
                    budgetSum2: "",
                    changeAmount: "",
                    remarks: "",
                    fileId: ""
                },
                itemValueList: [],
                operateType: ""
            },

            rules: {}
        };
    },

    computed: {
        applyDept2: function () {
            let types = getDict("budget_department");
            let pv = this.form.applyData.applyDept;
            if (pv && types.length > 0) {
                const pts = types.filter(t => t.value == pv);
                return pts ? pts[0].label : "";
            }
            return "";
        },

        applyType2: function () {
            let types = getDict("budget_type");
            let pv = this.form.applyData.applyType;
            if (pv && types.length > 0) {
                const pts = types.filter(t => t.value == pv);
                return pts ? pts[0].label : "";
            }
            return "";
        }
    },

    mounted () { },

    created () {
        const loading = this.$loading({
            lock: true,
            text: "数据加载中"
        });

        this.fileId = this.$route.query.fileId
        this.fileId2 = this.$route.query.fileId2
        setTimeout(() => {
            //通过id获取项目信息
            getObj(this.$route.query.applyId)
                .then(respData => {
                    this.$refs["add-form"].resetFields();
                    this.form.applyData = respData.data.data;
                    this.initItem(this.form.applyData.applyId);

                    //this.busId = respData.data.data.fileId;
                    //this.$refs.myApply.getFiles(this.busId);

                    loading.close();
                })
                .catch(e => {
                    loading.close();
                    this.$message({
                        type: "error",
                        message: "数据获取失败!"
                    });
                });
        }, 200);
    },

    methods: {
        // 科目
        initItem () {
            getItem2(this.form.applyData.applyId).then(rep => {
                let rspD = rep.data.data;
                if (rspD) {
                    rspD = rspD.map((rs, index) => {
                        rs["sortNum"] = index + 1;
                        return rs;
                    });
                }
                this.form.itemValueList = rspD;
            });
        },

        // 取消
        cancel () {
            this.closeNowRouter();
        },

        closeNowRouter () {
            //关闭当前新增页，暂时没找到好办法
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            //跳转
            this.$router.push({ name: "apply_list" });
        },

        // 下载
        handleDownload (fileId) {
            checkFiles(fileId).then(resp => {
                if (resp.data.code === 0) {
                    window.open("/admin/sys-file/download/" + fileId);
                } else {
                    this.$message({
                        message: `文件不存在！`,
                        type: "warning"
                    });
                }
            });
        }
    }
};
